package com.hyfd.service.mp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.GlobalSetHyfd;
import com.hyfd.common.Page;
import com.hyfd.dao.mp.AgentAccountChargeAuditDao;
import com.hyfd.dao.mp.AgentAccountChargeDao;
import com.hyfd.dao.mp.AgentAccountDao;
import com.hyfd.dao.mp.AgentDao;
import com.hyfd.service.BaseService;


@Service
public class AgentAccountChargeAuditSer extends BaseService {

	public Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private AgentAccountChargeAuditDao agentAccountChargeAuditDao;
	@Autowired
	AgentAccountDao agentAccountDao;
	@Autowired
	AgentAccountChargeDao agentAccountChargeDao;
	@Autowired
	AgentDao agentDao;

	public String agentAccountChargeAuditAdd(HttpServletRequest req) {
		Session session = getSession();
		try {
			boolean flag = false;
			Map<String, Object> myData = getMaps(req);
			String agentId = (String) myData.get("agentId");
			String type = (String) myData.get("type");
			String money = (String) myData.get("fee");
			double fee = Double.parseDouble(money);
			Map<String, Object> user = getUser();
			String userName = (String) user.get("suName");
			myData.put("applyUser", userName);
			Map<String, Object> agentAccount = agentAccountDao
					.selectByAgentid(myData);
			String ba = agentAccount.get("balance") + "";
			double balan = Double.parseDouble(ba);
			if (type.equals("1")) {
				int status = 0;
				myData.put("balance", balan);
				myData.put("flag", status);
				int rows = agentAccountChargeAuditDao
						.agentAccountChargeAuditAdd(myData);
				if (rows > 0) {
					flag = true;
				}
			}
			if (type.equals("2")) {
				Map<String, Object> m = new HashMap<String, Object>();
				UUID uuid = UUID.randomUUID();
				String id = uuid.toString().replace("-", "");
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String applyDate = df.format(new Date());
				Double balance = fee + balan;
				m.put("id", id);
				m.put("agentId", agentId);
				m.put("fee", fee);
				m.put("type", 5);
				m.put("balanceBefore", balan);
				m.put("balanceAfter", balance);
				m.put("applyDate", applyDate);
				myData.put("balance", balance);
				myData.put("updateUser", userName);
				int rows = agentAccountDao.updateByAgentIdSelective(myData);
				if (rows > 0) {
					int i = agentAccountChargeDao.insertSelective(m);
					int r = agentAccountChargeAuditDao.agentAccountChargeAuditAdd(myData);
					if (r > 0 && i > 0) {
						flag = true;
					}
				}
			}
			session.setAttribute(GlobalSetHyfd.backMsg, flag ? "提交成功" : "提交失败!");
		} catch (Exception e) {
			getMyLog(e, log); 
		}
		return "redirect:/agentListPage";
	}

	/**
	 * 根据条件分页获取代理商冲扣值列表数据并生成json
	 * 
	 * @param req
	 * @return
	 */
	public String agentAccountChargeAuditList(HttpServletRequest req) {
		StringBuilder sb = new StringBuilder();
		try {
			Map<String, Object> m = getMaps(req); // 封装前台参数为map
			Page p = getPage(m);// 提取分页参数
			int total = getAgentCount(m);
			p.setCount(total); 
			int pageNum = p.getCurrentPage();
			int pageSize = p.getPageSize();

			sb.append("{");
			sb.append("" + getKey("page") + ":" + p.getCurrentPage() + ",");
			sb.append("" + getKey("total") + ":" + p.getNumCount() + ",");
			sb.append("" + getKey("records") + ":" + p.getCount() + ",");
			sb.append("" + getKey("rows") + ":" + "");

			PageHelper.startPage(pageNum, pageSize);// mybatis分页插件
			List<Map<String, Object>> dataList = agentAccountChargeAuditDao
					.selectAgentAccountChargeAuditList(m);
			String dataListJson = BaseJson.listToJson(dataList);
			sb.append(dataListJson);
			sb.append("}");
		} catch (Exception e) {
			getMyLog(e, log);
		}

		return sb.toString();
	}

	/**
	 * 获取记录数量
	 * 
	 * @param m
	 * @return
	 */
	public int getAgentCount(Map<String, Object> m) {
		int agentCount = 0;
		try {
			agentCount = agentAccountChargeAuditDao.selectCount(m);
		} catch (Exception e) {
			getMyLog(e, log);
		}
		return agentCount;
	}

	public String agentAccountChargeAuditEdit(HttpServletRequest req) {
		Session session = getSession();
		try {
			boolean flag = false;
			Map<String, Object> myData = getMaps(req);
			String status = (String) myData.get("flag");
			String id = (String) myData.get("id");
			if (status.equals("1")) {
				String agentId = (String) myData.get("agentId");
				if (agentId != null && agentId != "") {
					String money = (String) myData.get("fee");
					double fee = Double.parseDouble(money);
					Map<String, Object> user = getUser();
					String users = (String) user.get("suName");
					myData.put("confirmUser", users);
					Map<String, Object> m = new HashMap<String, Object>();
					m.put("agentId", agentId);
					Map<String, Object> agentAccount = agentAccountDao
							.selectByAgentid(m);
					String ba = agentAccount.get("balance") + "";
					double balan = Double.parseDouble(ba);
					Double balance = fee + balan;
					m.put("balance", balance);
					m.put("updateUser", users);
					int rows = agentAccountDao.updateByAgentIdSelective(m);
					if (rows > 0) {
						Map<String,Object> param = new HashMap<String, Object>();
//						Map<String, Object> map = agentAccountChargeAuditDao.selectByPrimaryKey(id);
//						String mon =map.get("yue")+"";
//						double yueBefore = Double.parseDouble(mon);
//						Double yueAfter = fee + yueBefore;
						param.put("flag", status);
						param.put("yue", balance);
						param.put("id", id);
						param.put("confirmUser", users);
						Integer row = agentAccountChargeAuditDao.agentAccountChargeAuditEdit(param);
						if (row > 0 ) {
							Map<String, Object> accountMap = new HashMap<String, Object>();
							UUID uuid = UUID.randomUUID();
							String accountId = uuid.toString().replace("-", "");
							SimpleDateFormat df = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							String applyDate = df.format(new Date());
							accountMap.put("id", accountId);
							accountMap.put("agentId", agentId);
							accountMap.put("fee", fee);
							accountMap.put("type", 3);
							accountMap.put("balanceBefore", balan);
							accountMap.put("balanceAfter", balance);
							accountMap.put("applyDate", applyDate);
							accountMap.put("status", 3);
							int i = agentAccountChargeDao.insertSelective(accountMap);
							if(i>0){
								flag=true;
							}
						}
					}
				}
			} else {
				Integer row = agentAccountChargeAuditDao
						.agentAccountChargeAuditEdit(myData);
				if (row > 0) {
					flag = true;
				}
			}

			session.setAttribute(GlobalSetHyfd.backMsg, flag ? "审核成功" : "审核失败!");
		} catch (Exception e) {
			getMyLog(e, log);
		}
		return "agentAccountChargeAuditPage";
	}
	
	public String agentAccountChargeAuditEditPage(String id){
		try {
			Map<String, Object> agentAccountChargeAudit = agentAccountChargeAuditDao.selectByPrimaryKey(id);
			Session session = getSession();
			session.setAttribute("agentAccountChargeAudit", agentAccountChargeAudit);
		} catch (Exception e) {
			getMyLog(e, log);
		}
		return "mp/agentAccountChargeAuditEdit";
	}
	
	public String agentAccountChargeEditName(HttpServletRequest req){
		Session session = getSession();
		try {
			boolean flag = false;
			Map<String, Object> myData = getMaps(req);
			Integer i = agentAccountChargeAuditDao.agentAccountChargeEditName(myData);
			if(i>0){
				flag = true;
			}
			session.setAttribute(GlobalSetHyfd.backMsg, flag ? "修改成功" : "修改失败!");	
		} catch (Exception e) {
			getMyLog(e, log);
		}
		return "agentAccountChargeAuditPage";
	}
}
